// pages/pickDetails/pickDetails.js
var app = getApp();
var count = 0;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        modelShowC: true,
        modelShowR: true,
        modelShowZ: true,
        confirm: "请先收款",
        focus: false,
        textareaTitle: "请录入不接单原因",
        inputTitle: "请录入实际收到金额",
        insurance: false,
        itemDetail: "",
        id: "",
        money: 0,
        pickUpPayedMoney: 0,
        pickUpNums: 0,
        totalWeight: 0,
        totalVolume: 0,
        modelShowI: true,
        urlList: [{ name: "提货单", path: "", isOne: true }, { name: "装车前", path: "", isOne: false }, { name: "装车中", path: "", isOne: false }, { name: "装车后", path: "", isOne: false }, { name: "关车门", path: "", isOne: false }, { name: "盖雨棚", path: "", isOne: false   }],
        isOther: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onShow: function() {
      var that = this
        var itemDetail = app.globalData.driversOrders;
        console.log(itemDetail, "itemDetail")

        this.setData({
            itemDetail: app.globalData.driversOrders,
            id: itemDetail.id,
        })
        var pickUpNums = 0,
            totalWeight = 0,
            totalVolume = 0,
            pickUpPayedMoney = 0;
        for (let i = 0; i < itemDetail.orders.orderGoodInfosList.length; i++) {
            pickUpNums += itemDetail.orders.orderGoodInfosList[i].nums;
            pickUpPayedMoney += itemDetail.orders.orderGoodInfosList[i].freight;
            totalWeight += Number(itemDetail.orders.orderGoodInfosList[i].volumeWeight);
            totalVolume += Number(itemDetail.orders.orderGoodInfosList[i].volume)
        }
        // var insured = parseFloat(pickUpPayedMoney * itemDetail.orders.insuranceRate).toFixed(2) //保险金额计算
        // pickUpPayedMoney = parseFloat(Number(pickUpPayedMoney) + Number(insured)).toFixed(2)
        // this.setData({
        //     pickUpNums: pickUpNums,
        //     pickUpPayedMoney: pickUpPayedMoney.toFixed(2),
        //     totalWeight: totalWeight,
        //     totalVolume: totalVolume,
        //     insured: insured,
        //     otherFee: (Number(itemDetail.orders.otherFee) + Number(itemDetail.orders.packagingFee))
        // })
        that.setData({
          "urlList[0].path":itemDetail.goodsPicsList[0].filePath,
          "urlList[1].path": itemDetail.carBeforeLoadingPicsList[0].filePath,
          "urlList[2].path": itemDetail.carriageLoadsHalfPicsList[0].filePath,
          "urlList[3].path": itemDetail.carriageLoadsCompletePicsList[0].filePath,
          "urlList[4].path": itemDetail.closeDoorPicsList[0].filePath,
          "urlList[5].path": itemDetail.coverRainforestPicsList[0].filePath
        })
        
    },
    cargo(e) {
        var item = JSON.stringify(e.currentTarget.dataset.item)
        var idx = e.currentTarget.dataset.index
        app.globalData.orderid = e.currentTarget.dataset.item.orders.orderGoodInfosList[idx].id
        wx.navigateTo({
            url: "../cargork/cargork?item=" + item + "&index=" + e.currentTarget.dataset.index
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    cancel() {
        this.setData({
            modelShowC: true
        })
    },
    confirm() {
        this.setData({
            modelShowC: true,
            modelShowI: false
        })
    },
    zhuang() {
        console.log("幢")
        this.setData({
            modelShowZ: false
        })
        console.log(this.data.modelShowZ)
    },
    cancelR() {
        this.setData({
            modelShowR: true
        })
    },
    cancelZ() {
        this.setData({
            modelShowZ: true
        })
    },
    cancelI() {
        this.setData({
            modelShowI: true
        })
    },
    confirmI() {
        this.setData({
            modelShowI: true
        })
    },
    confirmR() {
        this.setData({
            modelShowR: true
        })
    },
    bindfocus() {
        this.setData({
            focus: true
        })
    },
    bindblur(e) {
        this.setData({
            money: e.detail.value
        })
    },
    reforder() {
        this.setData({
            modelShowR: false
        })
        console.log(this.data.modelShowR)
    },
    gathering() {
        console.log("1111")
        this.setData({
            modelShowI: false
        })
    },
    otherFee() { //查看其它费用
      var that = this
      that.setData({
        itemDetail: app.globalData.driversOrders,
        isOther: !that.data.isOther
      })
      // wx.navigateTo({
      //     url: "../other/other"
      // })
    },
    receiving: function() { //送达
        // this.setData({
        //     modelShowC: false
        // })
        var datas = {},
            item = app.globalData.driversOrders;
        console.log(item)
            // if (!app.getApi.drivers.vehicles_DriversList[0].vehicles.id) { //当车辆id不存在

        // }
        var vehicles = {};
        var vehiclesCount = 0;
        for (let i = 0; i < app.getApi.drivers.vehicles_DriversList.length; i++) {
            if (app.getApi.drivers.vehicles_DriversList[i].isVehicleUsing) {
                vehicles = app.getApi.drivers.vehicles_DriversList[i].vehicles;
                vehiclesCount++
            }
        }
        if (vehiclesCount <= 0) {
            wx.showToast({
                title: '未启用车辆',
                icon: 'loading',
                duration: 2000
            });
            return;
        }
        if (item.orders.payType == 3) { //送达需要收款
            if (item.orders.receivablesStatus != 1) {
                this.setData({
                    modelShowC: false,
                })
                return;
            }
        } else {

        }
        datas.deliveryOrderList = [{
            deliveryOrderStatusName: item.deliveryOrderStatusName,
            deliveryOrderStatus: item.deliveryOrderStatus,
            id: item.id
        }]
        datas.drivers = {
            id: app.getApi.drivers.id,
            name: app.getApi.drivers.name,
            tel: app.getApi.drivers.tel,
        }
        datas.vehicles = app.getApi.drivers.vehicles_DriversList[0].vehicles

        datas.ordersList = []
        for (let i = 0; i < item.orders.orderGoodInfosList.length; i++) {
            var newObj = item.orders.orderGoodInfosList[i].order

            datas.ordersList.push(newObj)
        }
        console.log(datas, "datas.ordersList")
        var that = this
        app.getApi.request("/api/DeliveryOrder/Sent", datas, "POST", (res) => {
            // , driversOrdersStatusName: e.currentTarget.dataset.statusname, driversOrdersStatus: e.currentTarget.dataset.status, pickUpNums: 0 
            console.log(res)
            if (res.data.responseCode == 1000) {

                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000,
                    success: function() {

                    }
                });
                setTimeout(function() {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 2000)
            } else {
                wx.showToast({
                    title: '提货失败',
                    icon: 'loading',
                    duration: 2000
                });
            }
        }, (res) => {})
    },
    checkboxChange(e) {
        console.log(e.detail.value[0])
        var value = e.detail.value[0]
        this.setData({
            insurance: !this.data.insurance
        })
        if (this.data.insurance) {
            this.setData({
                pickUpPayedMoney: parseFloat(Number(this.data.pickUpPayedMoney) + Number(this.data.insured)).toFixed(2)
            })
        } else {
            this.setData({
                pickUpPayedMoney: parseFloat(Number(this.data.pickUpPayedMoney) - Number(this.data.insured)).toFixed(2)
            })
        }
    },
    empty(e) {
        var index = e.currentTarget.dataset.index,
            imageList = this.data.urlList;

        imageList[index].path = ''
        this.setData({
            urlList: imageList
        })

    },
    receipt() {
        this.setData({
            modelShowZ: false,
            imageType: 4
        })
    },
    camera() {
        var that = this
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['camera'], // 指定来源是相机
            success: function(res) {
                var tempFilePaths = res.tempFilePaths
                that.setData({
                    modelShowZ: true
                })
                that.upLoadImage(tempFilePaths)
            }
        })

    },
    album() {
        var that = this
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album'], // 指定来源是相册
            success: function(res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths
                that.setData({
                    modelShowZ: true
                })
                that.upLoadImage(tempFilePaths)
            }
        })
    },
    upLoadImage(tempFilePaths, index) {
        var that = this,
            imageList = that.data.urlList;
        wx.uploadFile({
            url: 'https://driverAPI.dwilms.com/api/Upload/UploadOnePic', //仅为示例，非真实的接口地址
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {},
            success: function(res) {
                var data = JSON.parse(res.data)
                console.log(data)
                that.setData({
                    receiptImage: data.obj
                })
            }
        })
    },
    makePhoneCall(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.number
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})